<%+header%>
<h2><a id="content" name="content"><%:Package Infomation%></a></h2>
<script>  //general function
	function getValue(ID)
	{
		return document.getElementById(ID).value;
	}
	function setDisplay(ID,type)
	{
		if(!type)
		{
			document.getElementById(ID).style.display="none";
		}
		else
		{
			document.getElementById(ID).style.display="inline";
		}
	}
	function setHTML(ID,STR)
	{
		document.getElementById(ID).innerHTML=STR;
	}
	function getHTML(ID,STR)
	{
		return document.getElementById(ID).innerHTML;
	}
	function setValue(ID,val){
		document.getElementById(ID).value=val;	
	}
</script>

<script>
	
//get the URL parameters
function receive(){
    var thisURL=document.URL;
    var recVal=thisURL.split('?');
    if(recVal.length > 1){
        return recVal;	
    }
} 

function getpackage(Name){
	
		 setValue("ID_INPUT_submit",Name);
	   document.getElementById("ID_INPUT_submit").click();
}
		 
function getPackageList(){
	   var callPath='<%=luci.dispatcher.build_url("admin", "wifiscan","PackageList")%>';
		 var packListArr=new Array();
		 
     var DisplayPackageList=function (){
     	   if(0 >= packListArr.length ){
     	   	    return;
     	   }
     	   var listStr="";
     	   for(var i=0;i < packListArr.length;i++){
     	    listStr+='<tr>'
     	   		+'<td style="width:90%">'
     	   				+packListArr[i]
     	   		+'</td>'
     	   		+'<td>'
     	      		+'<input class="cbi-button" type="button" onclick="getpackage(\''+packListArr[i]+'\')"  value="<%:Download%>" \>'
     	   		+'</td>'
     	   +'</tr>';
     	 }
     	  setHTML("ID_Table_Packagelist",listStr);
     }
		 
	   XHR.get(callPath,null,function(x,rval){
	   	       if(null == rval){
	   	           	 deviceArr=new Array();
	   	       }else{
	   	           deviceArr=rval[0];
	   	       }
             
              packListArr=rval[0].split(/\n/);    //delete Enter symbol
              if("" == packListArr[packListArr.length-1]){
              	packListArr.splice(packListArr.length-1,1);
              }
              
	  	        setHTML("ID_Table_Packagelist","");
	  	        DisplayPackageList();
		     }
	   );
	   setHTML("ID_Table_Packagelist","<tr><td style='width:35%;'></td><td style='font-size:20px;color:red'><%:please wait a little......%></td></tr>");
	
}

var buttonStart="<%:start%>";
var buttonStop="<%:stop%>";

function ActStat(val){
	
		 if(buttonStart == val){
		     val="start";	
		 }else if(buttonStop == val){
		     val="stop";	
		 }
		 
		 var callPath='<%=luci.dispatcher.build_url("admin", "wifiscan","PackageAct")%>';
        callPath=callPath+'/'+val+'/'+URLParam[1]+'/'+URLParam[2];
	   XHR.get(callPath,null,function(x,rval){
	
	   	       switch(rval[0])
	   	       { 
	   	       	   case "runing":
	   	           case "stop":
	   	               setValue("ID_INPUT_GetPack",buttonStop);
	   	           break;	
	   	           case "unruning":
	   	           case "start":
	   	               setValue("ID_INPUT_GetPack",buttonStart);
	   	           break;
	   	           default:
	   	               alert("lua action is err!");
	   	           break;
	   	       }
		         getPackageList();
		     }
	   );
}

function clearpackage()
{
		 var callPath='<%=luci.dispatcher.build_url("admin", "wifiscan","PackageClear")%>';
	   XHR.get(callPath,null,function(x,rval){
	   	     getPackageList();
	   	});
	  
}

var URLParam=new Array();    
function init()
{
	URLParam=receive();
	ActStat("status");	
	getPackageList();
}
</script>

<p id="ID_P_wait"></p>
<fieldset class="cbi-section">
	<div>
		<input type="button" class="cbi-button" id="ID_INPUT_GetPack" onclick="ActStat(this.value)" value="<%:start%>">
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input class="cbi-button" type="button" value="<%:clear%>" onclick="clearpackage()" />
  </div>
  <br/>
  <div>
	<table width="100%" cellspacing="10" id="ID_Table_Packagelist">
	</table>
	<form style="display:none" method="post"  action="<%=luci.dispatcher.build_url("admin", "wifiscan","PackageGet")%>" enctype="multipart/form-data">
	  <input id="ID_INPUT_submit"  name="getpackage" type="submit"  \>
  </form>
  </div>
  <script>init();</script>
</fieldset>
<%+footer%>